package tools;
import lib.*;
public class MatchPeak {
	public static double[][]  matchExper(double[][] theor_peak_group,double[][] exper_peak_group,double win_len)
	{
		int array_max_length= exper_peak_group.length+theor_peak_group.length;
		
		//publicMethod.ArrayManipulation.printArrayT(exper_peak_group);
		//publicMethod.ArrayManipulation.printArrayT(theor_peak_group);
		double[] align_array1 = new double[array_max_length];
		double[] align_array2 = new double[array_max_length];
		double[] align_mz_all=new double[array_max_length];
		int index1 =0, index2 =0, index=0;
		double theor_max=0, exper_max=0;
		double STANDER=250;
		while(index1<exper_peak_group.length&&index2<theor_peak_group.length){
			if(exper_peak_group[index1][0]<theor_peak_group[index2][0]-win_len){
				align_mz_all[index]=exper_peak_group[index1][0];
				align_array1[index]=exper_peak_group[index1][1];
				align_array2[index++] = 0;
				
				if(exper_peak_group[index1][1]>exper_max)
				{
					exper_max=exper_peak_group[index1][1];
				}
				index1++;
			}
			else if(exper_peak_group[index1][0]-win_len > theor_peak_group[index2][0]){
				
				align_mz_all[index]=theor_peak_group[index2][0];
				align_array1[index] = 0;
				align_array2[index++]=-theor_peak_group[index2][1];
				if(theor_peak_group[index2][1]>theor_max)
				{
					theor_max=theor_peak_group[index2][1];
				}
				index2++;
				
			}
			else{
				align_mz_all[index]=exper_peak_group[index1][0];
				align_array1[index] = exper_peak_group[index1][1];
				align_array2[index++]=-theor_peak_group[index2][1];
				if(exper_peak_group[index1][1]>exper_max)
				{
					exper_max=exper_peak_group[index1][1];
				}
				if(theor_peak_group[index2][1]>theor_max)
				{
					theor_max=theor_peak_group[index2][1];
				}
				index1++;
				index2++;
			}
			
		}
		for(int i=index1;i<exper_peak_group.length;i++){
			align_mz_all[index]=exper_peak_group[i][0];
			align_array1[index] = exper_peak_group[i][1];
			align_array2[index++]=0;
			if(exper_peak_group[i][1]>exper_max)
			{
				exper_max=exper_peak_group[i][1];
			}
		}
		for(int i=index2;i<theor_peak_group.length;i++){
		    align_mz_all[index]=theor_peak_group[i][0];
			align_array1[index] = 0;
			align_array2[index++]=-theor_peak_group[i][1];
			if(theor_peak_group[i][1]>theor_max)
			{
				theor_max=theor_peak_group[i][1];
			}
		}
		
		for(int i=0;i<align_array1.length;i++)
		{
			align_array1[i]=align_array1[i]*STANDER/exper_max;
			align_array2[i]=align_array2[i]*STANDER/theor_max;
		}
		double[][] a = ArrayManipulation.array_trim(align_array1, align_array2);
		double[][] b={ArrayManipulation.array_trim(align_mz_all),a[0],a[1]};
		return b;
	}
	
}
